
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'SelectHorarioByPeriodo' AND user_name(uid) = 'dbo')
	DROP PROCEDURE dbo.SelectHorarioByPeriodo
GO

select *from horario
select *from detallematricula
select *from matricula

ALTER PROCEDURE dbo.SelectHorarioByPeriodo 1,3
(
  @periodo int,
  @persona int
)
AS

SELECT     
	Curso.nombre,
		CASE WHEN dia = 1 THEN Horario.hora_ini + '-' + Horario.hora_fin END AS LUNES,
		CASE WHEN dia = 2 THEN Horario.hora_ini + '-' + Horario.hora_fin END AS MARTES,
		CASE WHEN dia = 3 THEN Horario.hora_ini + '-' + Horario.hora_fin END AS MIERCOLES,
		CASE WHEN dia = 4 THEN Horario.hora_ini+ '-' + Horario.hora_fin END AS JUEVES,
		CASE WHEN dia = 5 THEN Horario.hora_ini+ '-' + Horario.hora_fin END AS VIERNES,
		CASE WHEN dia = 6 THEN Horario.hora_ini+ '-' + Horario.hora_fin END AS SABADO,
		CASE WHEN dia = 7 THEN Horario.hora_ini+ '-' + Horario.hora_fin END AS DOMINGO,
	Grupo.nombre AS nombre_grupo,
	Aula.Nombre AS nombre_aula
FROM         Curso INNER JOIN
                      DetalleMatricula ON Curso.codigo = DetalleMatricula.cod_curso INNER JOIN
                      Grupo ON Curso.codigo = Grupo.codigo INNER JOIN
                      Horario ON Curso.codigo = Horario.cod_curso AND Grupo.codigo = Horario.cod_grupo INNER JOIN
                      Matricula ON DetalleMatricula.cod_matricula = Matricula.codigo INNER JOIN
                      Aula ON Horario.cod_aula = Aula.codigo
WHERE
	Matricula.cod_periodo = @periodo and
	Matricula.cod_persona = @persona

GO
